package gimys;

import java.util.ArrayList;
import java.util.LinkedList;

import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Circle;

public class Player extends SolidObject {

	private static int MAX_SKINS = 5;
	
	private ArrayList<SpriteSheet> sprites;
	private LinkedList<Skin> skins;
	
	private int seductionLevel;
	private int maxSeductionLevel;
	private int seductionTimer;
	
	public boolean hasMoved = false;

	protected boolean died = false;
	
	public Player(Point pLocation) throws SlickException {
		super();
		this.deltaYSprite = -16;
		this.sprite = new SpriteSheet("data/Player.png", 16, 32);
		this.sprites = new ArrayList<SpriteSheet>();
		for (int i = 0; i < 4; i++) {
			this.sprites.add(new SpriteSheet("data/Player" + i + ".png", 16, 32));
		}
		this.collisionShape = new Circle(pLocation.x+8, pLocation.y+8, 8);
		this.skins = new LinkedList<Skin>();
		
		this.type = Type.PLAYER;
		this.speed = 0.1f;
		this.maxSeductionLevel = 100;
		this.seductionLevel = 100;
		this.seductionTimer = 0;
	}
	
	public float getSeductionLevel() {
		return this.seductionLevel;
	}
	
	/***** Skin management ****/
	
	public Skin getSkin() {
		if (!skins.isEmpty()) {
			return this.skins.get(0);
		}
		else return null;
	}

	public Skin getSkin(int i) {
		if (skins.size() > i) {
			return this.skins.get(i);
		}
		else return null;
	}

	public boolean destroyCurrentSkin() {
		if (!skins.isEmpty()) {
			skins.removeFirst();
		}
		if ( skins.isEmpty() ){
			died = true;
		}
		return skins.isEmpty();
	}
	
	public int getNumberOfSkins() {
		return this.skins.size();
	}

	public void addSkin(Skin r) {
		if (this.skins.size() > Player.MAX_SKINS) {
			this.skins.removeLast();
		}
		this.skins.addFirst(r);
	}
	
	/***** Collision handlers *****/
	
	public void collisionHandler(Animal animal) {
		if (animal.getSkin() != this.getSkin()) {
			if (animal.getAttackTimer() > 3000) {
				System.out.println("Player touched !");
				if (this.destroyCurrentSkin()) {
					this.died = true;
				}
				animal.resetAttackTimer();
			}
		}
	}
	
	
	/***** Seduction management ****/
	
	public void increaseSeductionLevel() {
		if (this.seductionLevel < this.maxSeductionLevel) {
			this.seductionLevel++;
		}
		//System.out.println("Seduction level " + this.seductionLevel);
	}
	
	public boolean useSeductionLevel(int quantity) {
		if (this.seductionLevel - quantity >= 0) {
			this.seductionLevel -= 25;
			return true;
		}
		else {
			return false;
		}
	}

	public Image getImage() {
		if (hasMoved) {
			if (getNumberOfSkins() == 0) {
				return this.sprite.getSubImage((this.count /200)% 2, this.direction.ordinal());
			}
			return this.sprites.get(this.skins.get(0).ordinal()).getSubImage((this.count /200)% 2, this.direction.ordinal());
		} else {
			if (getNumberOfSkins() == 0) {
				return this.sprite.getSubImage(0, this.direction.ordinal());
			}
			return this.sprites.get(this.skins.get(0).ordinal()).getSubImage(0, this.direction.ordinal());
		}
	}

	public Image getGuiSkin(int numSkin) {
		return sprites.get( this.skins.get(numSkin).ordinal() ).getSubImage(0, 2);
	}
	/****** Movement and collision management *****/
	
	public void update(int delta) {
		this.count++;
		// Increase seduction level
		this.seductionTimer += delta;
		if (this.seductionTimer > 10000) {
			this.seductionTimer = 0;
			this.increaseSeductionLevel();
		}
	}
}
